using rbac.contracts.Dto.Input;
using rbac.contracts.Dto.Output;
using rbac.contracts.Interface;
using rbac.domain.Entity;
using rbac.domain.Interface;

namespace rbac.services;
/// <summary>
/// 111
/// </summary>
public class PermissionService:ServiceBase,IPermissionService
{
    private readonly IPermissionRepository _permissionRepository;

    public PermissionService(IPermissionRepository permissionRepository)
    {
        _permissionRepository = permissionRepository;
    }

    public PageOutput<PermissionOutput> QueryPermission(QueryPermissionInput input)
    {
        var query = _permissionRepository.Permissions();
        if (!string.IsNullOrWhiteSpace(input.PermissionName))
        {
            query = query.Where(p => p.PermissionName.Contains(input.PermissionName));
        }

        var result = new PageOutput<PermissionOutput>
        {
            Total = query.Count(),
            PageSize = input.PageSize
        };
        var list = query.OrderBy(p => p.Id).Skip((input.PageIndex - 1) * input.PageSize).Take(input.PageSize);
        result.Data = _mapper.Map<List<PermissionOutput>>(list);

        return result;
    }

    public int Add(PermissionInput input)
    {
        var permission = _mapper.Map<Permission>(input);
        permission.CreatedUserId = LoginUserId;
        permission.UpdatedUserId = LoginUserId;
        return _permissionRepository.Add(permission);
    }

    public int Update(PermissionInput input)
    {
        var permission = _mapper.Map<Permission>(input);
        permission.UpdatedUserId = LoginUserId;

        return _permissionRepository.Update(permission);
    }

    public int Delete(params int[] id)
    {
        return _permissionRepository.Delete(LoginUserId, id);
    }
}